import os
from celery import Celery


# 与 manage.py 保持一致的 settings 路径
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "code_env_spaces.setting.dev")


celery_app = Celery("code_env_spaces")

# 从 Django settings 中读取以 CELERY_ 开头的配置
celery_app.config_from_object("django.conf:settings", namespace="CELERY")

# 自动发现各 Django app 下的 tasks.py
celery_app.autodiscover_tasks()

# 可选：Celery Beat 兜底扫描任务（每10分钟）
try:
    from celery.schedules import crontab
    celery_app.conf.beat_schedule = {
        "orders-sweep-unpaid-every-10min": {
            "task": "orders.sweep_unpaid_orders",
            "schedule": crontab(minute="*/10"),
            "options": {"queue": "orders"},
            "args": (),
        },
    }
except Exception:
    pass


__all__ = ["celery_app"]

